package com.monitor.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.monitor.dao.SmsShieldWordDao;
import com.monitor.po.SmsShieldWord;
import com.monitor.util.Pager;

@SuppressWarnings("unchecked")
public class SmsShieldWordDaoImpl implements SmsShieldWordDao {
	private SessionFactory sessionFactory;

	@Override
	public List<SmsShieldWord> getAllSmsShiedWords() throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("from SmsShieldWord").list();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getAllSmsShiedWords()....");
		}
	}

	@Override
	public Pager<SmsShieldWord> getSmsShieldWords(int currentPage, int pageSize) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from SmsShieldWord");
			int totalCount = ((Number) query.uniqueResult()).intValue();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
			List<SmsShieldWord> list = sesion.createQuery("from SmsShieldWord s order by s.addTime desc").setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
			Pager<SmsShieldWord> pager = new Pager<SmsShieldWord>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getSmsShieldWords()....");
		}
	}

	@Override
	public void addSmsShieldWord(String... words) throws Exception {
		if (words == null || words.length <= 0) {
			return;
		}
		try {
			StringBuffer sqlBuffer = new StringBuffer("insert ignore into monitor_sms_shield_word(id,word,addTime) values ");
			int size = words.length;
			for (int i = 0; i < size; i++) {
				sqlBuffer.append("(?,?,?)");
				if (i != size - 1) {
					sqlBuffer.append(",");
				}
			}
			SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sqlBuffer.toString());
			for (int i = 0; i < size; i++) {
				String keyword = words[i];
				int id = keyword.hashCode();
				query.setInteger(3 * i + 0, id);
				query.setString(3 * i + 1, keyword);
				query.setLong(3 * i + 2, System.currentTimeMillis());
			}
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting addSmsShieldWord()....");
		}
	}

	@Override
	public int deleteSmsShieldWord(int[] ids) throws Exception {
		try {
			StringBuffer sql = new StringBuffer("delete from SmsShieldWord s where");
			int length = ids.length;
			for (int i = 0; i < length; i++) {
				sql.append(" s.id=?");
				if (i != length - 1) {
					sql.append(" or ");
				}
			}
			Query query = sessionFactory.getCurrentSession().createQuery(sql.toString());
			for (int i = 0; i < length; i++) {
				int id = ids[i];
				query.setInteger(i, id);
			}
			return query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting deleteSmsShieldWord()....");
		}
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
